package com.sapitest.script;

import com.alibaba.fastjson.JSON;
import com.sapitest.imp.TestCases;
import com.sapitest.imp.TestData;
import com.sapitest.keywords.DBOperation;
import com.sapitest.keywords.JSONOperation;
import com.sapitest.keywords.LinuxOperation;
import com.sapitest.keywords.asserts.BaseAssert;
import com.sapitest.keywords.asserts.JSONAssert;
import com.sapitest.utils.ConfigFile;
import com.sapitest.utils.bean.Response;
import com.sapitest.utils.redis.Redis;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.List;

import static com.sapitest.keywords.HTTPClient.sendHttpRequestXML;

public class Testcase1 {
    private static String mysqlDriver = ConfigFile.getTestParameter("mysqlDriver");
    private static String mysqlUrl = ConfigFile.getTestParameter("mysqlUrl");
    private static String mysqlUser = ConfigFile.getTestParameter("mysqlUser");
    private static String mysqlPassword = ConfigFile.getTestParameter("mysqlPassword");

    @BeforeClass
    public void beforeClass(){
//        RedisOperation redisOperation = new RedisOperation();
//        String result = redisOperation.redisHandle("","",6327,"","");
    }

    @Test
    public void testcase01(){
//        Response res = sendHttpRequest(ConfigFile.getTestParameter("test_url"), "post","[{\"Content-type\": \"application/x-www-form-urlencoded\"}, {\"User-Agent\":\"Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2\"}]","");
//        Assert.assertEquals(200, res.getResponseCode());
//        Assert.assertNotNull(res.getResponseBody());
        List list = DBOperation.dbQuery(mysqlDriver, mysqlUrl, mysqlUser, mysqlPassword, "select * from dogetcase;", 3);
        int i = DBOperation.dbUpdate(mysqlDriver, mysqlUrl, mysqlUser, mysqlPassword, "update student set name='小叉叉', birthday='1994-12-12' where id = 9", 3);
        Assert.assertEquals("", list.toString());
        Assert.assertEquals(i, 1);
    }

    @Test
    public void testcase02(){
        Response res = sendHttpRequestXML("http://www.baidu.com/","get","","");
        Assert.assertEquals(200, res.getResponseCode());
        Assert.assertNotNull(res.getResponseBody());
        String result = LinuxOperation.execLinuxCommand("root", "123456", "192.168.245.130", 22, "pwd");
        Assert.assertEquals("/root\n", result);
        String result2 = LinuxOperation.execOutToFile("root", "123456", "192.168.245.130", 22, "pwd\n", "D:\\Java\\test3.txt");
    }

    @Test
    public void testcase03(){
//        JSONAssert.jsonObjectEquals("{\"aa\":\"1\",\"bb\":[{\"cc\":\"2\"},{\"dd\":\"3\"}],\"e\":[4,5,6]}", "{\"aa\":\"1\", \"e\":[4,5,6],\"bb\":[{\"cc\":\"2\"},{\"dd\":\"3\"}]}");
//        JSONAssert.jsonObjectStrictEquals("{\"aa\":\"1\",\"bb\":\"2\"}", "{\"aa\":\"1\",\"bb\":\"2\"}");
//        JSONAssert.jsonArrayEquals("[{\"aa\":\"1\",\"bb\":\"2\"},[{\"cc\":\"3\",\"dd\":\"4\"}]]","[{\"bb\":\"2\",\"aa\":\"1\"},[{\"dd\":\"4\",\"cc\":\"3\"}]]");
        JSONAssert.jsonObjectEqualsIgnoreKeys("{\"aa\":\"1\",\"bb\":\"2\",\"cc\":\"3\",\"dd\":\"4\"}","{\"aa\":\"1\",\"bb\":\"2\"}","cc,bb,dd");
//        JSONAssert.jsonObjectSize("{\"aa\":\"1\",\"bb\":\"2\"}", 2);
    }

    @Test
    public void testcase04(){
        String aa = JSONOperation.getValue("{\"aa\":\"1\",\"bb\":\"2\"}","aa");
        Assert.assertEquals("aa", aa);
    }

    @Test
    public void testcase05(){
        Response response = TestCases.testGetGoodsList(1);
        BaseAssert.numEquals(response.getResponseCode(), 200);
        JSONAssert.jsonObjectEquals(response.getResponseBody(), "");
    }

    @Test
    public void testcase06(){
        Response response = TestCases.testGetGoodsListWithoutPara(1, "version,lang");
        BaseAssert.numEquals(response.getResponseCode(), 200);
        JSONAssert.jsonObjectEquals(response.getResponseBody(), "");
    }

    @Test
    public void testcase07(){
        String ss = TestData.getGoodsListRequestData(1);
        String dd = JSONOperation.fluentRemove(ss, "imei");
        String ee = JSONOperation.getJSONObjectString(ss, "devicesInfo");
        String rr = JSONOperation.fluentRemove(ee, "meid");
        String tt = JSONOperation.fluentPut(ss, "devicesInfo", JSON.parseObject(rr));
        BaseAssert.strEquals(tt, "");
    }

    @Test
    public void testcase08(){
        String ss = TestData.myShoppingTrolleyRequestData(1);
        BaseAssert.strEquals(ss, "");
    }

    @Test
    public void testcase09(){
        Response response = TestCases.testMyShoppingTrolley(1);
        BaseAssert.numEquals(response.getResponseCode(), 200);
        JSONAssert.jsonObjectEquals(response.getResponseBody(), "");
    }

    @Test
    public void testcase10(){
        Response response = TestCases.testMyShoppingTrolleyWithoutPara(1, "version,lang");
        BaseAssert.numEquals(response.getResponseCode(), 200);
        JSONAssert.jsonObjectEquals(response.getResponseBody(), "");
    }

    @Test
    public void testcase11(){
        String res1 = TestData.getExpResponseData(ConfigFile.getTestParameter("GET_GOODS_LIST_TABLE"),4);
        String res2 = TestData.getExpResponseData(ConfigFile.getTestParameter("MY_SHOPPING_TROLLEY_TABLE"),4);
        JSONAssert.jsonObjectEquals(res1, res2);
    }

    @Test
    public void testcase12(){
        Redis redisUtilV2 = Redis.newInstance("192.168.245.130",null,6379);
        BaseAssert.strEquals(redisUtilV2.execCommand("zrangeByScope", "rank", "1000", "1500"), "[26008600000001, 26008600000003, 26008600000002]");
    }

}
